package com.simon.train.business.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.simon.train.business.domain.DailyTrainStation;
import com.simon.train.business.domain.DailyTrainStationExample;
import com.simon.train.business.domain.TrainStation;
import com.simon.train.business.dto.DailyTrainStationQueryDto;
import com.simon.train.business.dto.DailyTrainStationSaveDto;
import com.simon.train.business.mapper.DailyTrainStationMapper;
import com.simon.train.business.vo.DailyTrainStationQueryVO;
import com.simon.train.common.util.SnowUtil;
import com.simon.train.common.vo.PageVO;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
public class DailyTrainStationService {

    private static final Logger LOG = LoggerFactory.getLogger(DailyTrainStationService.class);

    @Resource
    private DailyTrainStationMapper dailyTrainStationMapper;

    @Resource
    private TrainStationService trainStationService;

    public void SaveDailyTrainStation(DailyTrainStationSaveDto dailyTrainStationSaveDto){
        DailyTrainStation dailyTrainStation = BeanUtil.copyProperties(dailyTrainStationSaveDto, DailyTrainStation.class);
        if (ObjectUtil.isNull(dailyTrainStation.getId())){
            //对于首次保存 也就是新增 需要对字段单独做处理
            dailyTrainStation.setId(SnowUtil.getSnowflakeNextId());
            DateTime time = DateTime.now();
            dailyTrainStation.setCreateTime(time);
            dailyTrainStation.setUpdateTime(time);
            dailyTrainStationMapper.insert(dailyTrainStation);
        } else {
            dailyTrainStation.setUpdateTime(DateTime.now());
            dailyTrainStationMapper.updateByPrimaryKey(dailyTrainStation);
        }

    }

    public PageVO<DailyTrainStationQueryVO> QueryDailyTrainStationList(DailyTrainStationQueryDto dailyTrainStationQueryDto){
        DailyTrainStationExample dailyTrainStationExample = new DailyTrainStationExample();
        dailyTrainStationExample.setOrderByClause("date desc, train_code asc, `index` asc");
        DailyTrainStationExample.Criteria criteria = dailyTrainStationExample.createCriteria();

        if(ObjectUtil.isNotEmpty(dailyTrainStationQueryDto.getCode())){
            criteria.andTrainCodeEqualTo(dailyTrainStationQueryDto.getCode());
        }

        LOG.info("当前请求的页码：{}", dailyTrainStationQueryDto.getPage());
        LOG.info("每页条数：{}", dailyTrainStationQueryDto.getPageSize());

        PageHelper.startPage(dailyTrainStationQueryDto.getPage(),dailyTrainStationQueryDto.getPageSize());
        List<DailyTrainStation> dailyTrainStationList = dailyTrainStationMapper.selectByExample(dailyTrainStationExample);

        PageInfo<DailyTrainStation> pageInfo = new PageInfo<>(dailyTrainStationList);
        LOG.info("总行数：{}", pageInfo.getTotal());
        LOG.info("总页数：{}", pageInfo.getPages());

        List<DailyTrainStationQueryVO> lists = BeanUtil.copyToList(dailyTrainStationList, DailyTrainStationQueryVO.class);
        PageVO<DailyTrainStationQueryVO> pageVO = new PageVO<>();
        pageVO.setList(lists);
        pageVO.setTotal(pageInfo.getTotal());

        return pageVO;
    }

    public void deleteDailyTrainStation(Long id) {
        dailyTrainStationMapper.deleteByPrimaryKey(id);
    }

    /**
     * 生成date日期的trainCode的车次车站的数据
     * @param trainCode
     * @param date
     */
    public void GenerateDailyTrainStation(String trainCode, Date date){
        LOG.info("生成日期【{}】的车次【{}】车站信息开始", DateUtil.formatDate(date),trainCode);

        //1.删除现有的该车次对应的所有的车站信息
        DailyTrainStationExample dailyTrainStationExample = new DailyTrainStationExample();
        dailyTrainStationExample.createCriteria()
                .andTrainCodeEqualTo(trainCode)
                .andDateEqualTo(date);
        dailyTrainStationMapper.deleteByExample(dailyTrainStationExample);

        //2.去trainStation中查出所有的车站信息 生成到每日车站表中
        List<TrainStation> trainStations = trainStationService.SelectByTrainCode(trainCode);

        if(CollUtil.isEmpty(trainStations)){
            LOG.info("改车次没有车站信息，生成结束");
            LOG.info("生成日期【{}】的车次【{}】车站信息结束", DateUtil.formatDate(date),trainCode);
            return;
        }

        for (TrainStation trainStation : trainStations) {
            DailyTrainStation dailyTrainStation = BeanUtil.copyProperties(trainStation, DailyTrainStation.class);
            dailyTrainStation.setId(SnowUtil.getSnowflakeNextId());
            DateTime time = DateTime.now();
            dailyTrainStation.setCreateTime(time);
            dailyTrainStation.setUpdateTime(time);
            dailyTrainStation.setDate(date);
            dailyTrainStationMapper.insert(dailyTrainStation);
        }

        LOG.info("生成日期【{}】的车次【{}】车站信息结束", DateUtil.formatDate(date),trainCode);
    }


}
